package com.dddframework.mq.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;

/**
 * @author Liyj
 */
@Slf4j
public abstract class RedisMQListener implements IMessageListener<String, String, ObjectRecord<String, String>> {

    @Override
    public void onMessage(ObjectRecord<String, String> objectRecord) {
        RecordId recordId = objectRecord.getId();
        String key = objectRecord.getStream();
        log.debug("\n------{}  接收数据:\n recordId={}; value={}", this.getClass().getSimpleName(), recordId.getValue(), objectRecord);
        try {
            String value = objectRecord.getValue();
            int indexOf = value.indexOf("{");
            if (indexOf > 0) {
                value = value.substring(indexOf);
            }
            this.receive(value);
        } catch (Exception e) {
            log.error("{} 处理异常", this.getClass().getName(), e);
        }
    }
}
